package com.bootcamp.javase2.synchro;

import java.io.*;
import java.text.DecimalFormat;

/**
 * @program: day20Sycn
 * @description: implement Runnable to copy two files
 * @author: Keyu Li
 * @create: 2021-09-14 19:32
 **/

public class CopyRunnable {
    static final String SRC_FILR = "D:/wd/copythreads/jiyu.exe";
    static final String DST_DIR = "D:/wd/copythreads/";
    static final int THREAD_NUM = 5;

    public static void main(String[] args) throws IOException {
        // get src file size
        File srcFile = new File(SRC_FILR);
        double srcSize = srcFile.length() * 1.0 / (1 << 20);
        System.out.println("file " + srcFile.getName() + " size is "
                + new DecimalFormat("#.00").format(srcSize) + " MB");

        // get dst file name: jiyu_0.exe
        String[] dst = new String[THREAD_NUM];
        for (int i = 0; i < THREAD_NUM; i++) {
            dst[i] = SRC_FILR.replace(".exe", "_") + i + ".exe";
        }
        // create thread 0 ~ THREAD_NUM (5)
        for (int i = 0; i < THREAD_NUM; i++) {
            new Thread(new FileCopyRun(srcFile, dst[i], srcSize), "Thread" + i).start();
        }
    }
}

class FileCopyRun implements Runnable {
    FileInputStream in;
    FileOutputStream out;
    // 1 MB per write method.
    byte[] bytes = new byte[1 << 20];
    // store total size of thh src file
    double totalSize;

    public FileCopyRun(File src, String dst, double totalSize) {
        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(dst);

        } catch (IOException e) {
            e.printStackTrace();
        }
        this.totalSize = totalSize;
    }

    @Override
    public void run() {
        int count = 0;
        try {
            while (in.read(bytes) != -1) {
                out.write(bytes);
                count++;
                // copy success
                if (count >= totalSize) break;
                System.out.println(Thread.currentThread().getName()
                        + " has already copied [ " + count + " MB "
                        + new DecimalFormat("#.00").
                        format(count * 100 / totalSize)
                        + "% ]");
            }
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName() + " Copy success!");
    }
}


class FileCopyThread extends Thread {
    FileInputStream in;
    FileOutputStream out;
    byte[] bytes = new byte[1 << 20];
    double totalSize;


    public FileCopyThread(String name, File src, String dst,
                          double totalSize) throws FileNotFoundException {
        super(name);
        in = new FileInputStream(src);
        out = new FileOutputStream(dst);
        this.totalSize = totalSize;
    }

    @Override
    public void run() {
        int count = 0;
        int flag = -1;
        try {
            while ((flag = in.read(bytes)) != -1) {
                out.write(bytes);
                count++;
                if (count >= totalSize) break;
                System.out.println(this.getName()
                        + " has already copied [ " + count + " MB "
                        + new DecimalFormat("#.00").
                        format(count * 100 / totalSize)
                        + "% ]");
            }
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(this.getName() + " Copy success!");
    }
}
